//
//  PhotoListTableViewController.m
//  PrintFamily
//
//  Created by edaihua on 16/3/27.
//  Copyright © 2016年 ___PrintFactory___. All rights reserved.
//

#import "PhotoListTableViewController.h"
#import "PhotoListTableViewCell.h"
#import "PhotoPickerCollectionViewController.h"
#import "ConfirmOrderViewController.h"
#import "PFPrintManager.h"
#import "PhotoResizerViewController.h"

@interface PhotoListTableViewController ()

@property (strong, nonatomic) IBOutlet UIButton *rightButton;
@property (strong, nonatomic) IBOutlet UIButton *leftButton;

@end

@implementation PhotoListTableViewController
@synthesize photosManager;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightButton];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.leftButton];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.photosManager getPhotoCount];
;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PhotoListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
    PFPhoto *photo = [self.photosManager getPhotoAtIndex:indexPath.row];
    [cell initWithPhoto:photo delegate:self andPrintType:[PFPrintManager getCurrentType]];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return [PFCommonRectManager getMasterCellSizeWithType:[PFPrintManager getCurrentType]].height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    
    PhotoResizerViewController *photoResizer = [storyboard instantiateViewControllerWithIdentifier:@"PhotoResizer"];
    photoResizer.photoManager = self.photosManager;
    photoResizer.photo        = [self.photosManager getPhotoAtIndex:indexPath.row];
    photoResizer.tableView    = tableView;
    
    [self.navigationController presentViewController:photoResizer animated:YES completion:^{
        
    }];
}

#pragma mark - button gesture
- (IBAction)confirmTapped:(id)sender {
    // Go to Confirm page
    [self performSegueWithIdentifier:@"showConfirmView" sender:sender];
}

- (IBAction)continueAddTapped:(id)sender {

    for (UIViewController* viewController in self.navigationController.viewControllers) {
        if ([viewController isKindOfClass:[PhotoPickerCollectionViewController class]]) {
            NSLog(@"Found photo picker");
            PhotoPickerCollectionViewController *photoPicker = (PhotoPickerCollectionViewController*)viewController;
            
            photoPicker.selectedAssets = [self.photosManager getSelectedAssets];
        }
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Segues
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"popupPhotoPicker"]) {
        PhotoPickerCollectionViewController *photoPicker = segue.destinationViewController;
        photoPicker.rectManager = self.commonRectManager;
        photoPicker.photosManager = self.photosManager;
    } else if ([segue.identifier isEqualToString:@"showConfirmView"]) {
        ConfirmOrderViewController *vc = segue.destinationViewController;
        
    }
}


#pragma mark - synthethizer
- (PFPhotosManager*)photosManager {
    if (photosManager == nil) {
        photosManager = [[PFPhotosManager alloc] initFromLocalStorage];
    }
    return photosManager;
}


@end
